package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Sorting;
import com.itheima.service.SortingService;
import org.springframework.web.bind.annotation.*;

import javax.ws.rs.Path;
import java.util.List;

/**
 * @author 吴
 * @version V1.0
 * @Description  分院控制层
 * @Package com.itheima.controller
 * @date 2021/12/8 15:01
 */

@RestController
@RequestMapping("/sorting")
public class SortingController {

    @Reference
    private SortingService sortingService;

    /**
     * 添加
     * @param sorting
     * @return
     */
    @PostMapping("/save")
    public Result saveSorting(@RequestBody Sorting sorting){
        try {
            sortingService.saveSorting(sorting);
            return new Result(true, MessageConstant.ADD_SORTING_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_SORTING_FAIL);
        }
    }

    @GetMapping("/findPage")
    public Result saveSorting(QueryPageBean queryPageBean){
        try {
            PageResult result = sortingService.selectByCondition(queryPageBean);
            return new Result(true, MessageConstant.SELECT_SORTING_SUCCESS,result);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.SELECT_SORTING_FAIL);
        }
    }

    @GetMapping("/edit/{id}")
    public Result editSorting(@PathVariable("id") Integer id){
        try {
            Sorting sorting = sortingService.selectById(id);
            return new Result(true, MessageConstant.SELECT_SORTING_SUCCESS,sorting);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.SELECT_SORTING_FAIL);
        }
    }

    @PutMapping("/update")
    public Result updateSorting(@RequestBody Sorting sorting){
        try {
            sortingService.update(sorting);
            return new Result(true, MessageConstant.EDIT_SORTING_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_SORTING_FAIL);
        }
    }

    @DeleteMapping("/delete/{id}")
    public Result deleteSorting(@PathVariable("id") Integer id){
        try {
            sortingService.delete(id);
            return new Result(true, MessageConstant.DELETE_SORTING_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_SORTING_FAIL);
        }
    }

    /**
     * 查询全部的分院信息
     * @return
     */
    @GetMapping("/findAll")
    public Result findAll(){
        try {
            List<Sorting> list = sortingService.findSortingAll();
            return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }
}
